import Layout from './components/Layout'

export class Module {
  module = {
    layout: Layout,
    routes: [],
  };


  layout(layout = null) {
    //set
    if (layout !== null) {
      this.module.layout = layout;
      return this;
    }
    //get
    return this.module.layout;
  }

  routes(routes = null) {
    //set
    if (routes !== null) {
      this.module.routes = this.module.routes.concat(routes);
      return this;
    }
    //get

    // let routesValue = [
    //   {
    //     path: '/',
    //     component: Layout,
    //     children: [
    //       ...this.module.routes
    //     ],
    //   }
    // ];
    let routesValue = [
      ...this.module.routes
    ];
    return routesValue;
  }

  static module(name) {
    let module = new this();
    return module;
  }

}

export class ModuleManager {
  static* modules() {
    let modules = require.context('@/applications', true, /module\.js$/);
    for (let index = 0; index < modules.keys().length; index++) {
      let item = modules.keys()[index];
      yield modules(item).default;
    }
  }

  static routes() {


    let moduleRoutes = [];
    for (let module of ModuleManager.modules()) {
      moduleRoutes = moduleRoutes.concat(module.routes());
    }
    let routeroot = [
      {
        path: '/',
        component: Layout,
        children: [
          ...moduleRoutes
        ],
      }
    ];
    return routeroot;
  }
}
